package com.rolerealm.controller;

import com.rolerealm.common.result.Result;
import com.rolerealm.common.util.JwtUtil;
import com.rolerealm.common.util.ResultUtil;
import com.rolerealm.domain.dto.UserLoginRequest;
import com.rolerealm.domain.dto.UserRegisterRequest;
import com.rolerealm.domain.vo.LoginResponseVO;
import com.rolerealm.domain.vo.UserInfoVO;
import com.rolerealm.domain.vo.UserSimpleVO;
import com.rolerealm.service.UserService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/users")
public class UsersController {

    @Autowired
    private  UserService userService;

    @Autowired
    private  JwtUtil jwtUtil;

    @PostMapping("/register")
    public Result<UserSimpleVO> register(@Valid @RequestBody UserRegisterRequest req) {
        return ResultUtil.success(userService.register(req));
    }

    @PostMapping("/login")
    public Result<LoginResponseVO> login(@Valid @RequestBody UserLoginRequest req) {
        return ResultUtil.success(userService.login(req));
    }

    @GetMapping("/me")
    public Result<UserInfoVO> me(@RequestHeader(name = "Authorization", required = false) String authorization) {
        Long userId = jwtUtil.extractUserIdFromAuthorization(authorization);
        return ResultUtil.success(userService.getCurrentUser(userId));
    }
}


